var search_box = {
    
    
	version : 0.01,
	json_subjectarealist: "/modules/search_box/json_subjectarealist.php",
	category_combo : "#sel_category_area",
	category : -1,
	category_data : {"Categories" : {
	                    "a" : {"text": "- - Select from ... - - ", "value": "0", "area": "courses"},
		                "school_leavers" : {"text" : "School leavers", "value": "School Leavers", "area": "courses/school_leavers"},
		                "UCP" : {"text" : "Degree courses", "value": "Degree Courses", "area": "UCP", "disabled":true},
		                "apprenticeships" : {"text" : "Apprenticeship centre", "value": "Apprenticeship Centre", "area": "courses/apprenticeships", "disabled":true},
		                "adult_learning" : {"text" : "Adult learning", "value": "Adult Learners", "area": "courses/adult_learning"},
		                "business_employer" : {"text" : "Business & employers", "value": "Business and Employers", "area": "courses/business_employer"},
		                "international" : {"text" : "International courses", "value": "International Courses", "area": "courses/international"}
		}
	},
	filter_options : {"options" : 
							[{"text": "All", "value": ""},
	                         {"text": "Full Time", "value": "FT"},
	                          {"text": "Part Time", "value": "PT"}]
		
	},
	no_prints : {"no_print_areas" : [	"#header_bg_right",
	                                	"#bg_left",
	                                	"#menu_top",
	                                	"#submenu_holder",
	                                	"#vinnerbar","#vbar",
	                                	"#arc_box",
	                                	"#section_header",
	                                	"#search_box",
	                                	"#accessiblity_area",
	                                	"#footer",
	                                	"#info_bar",
	                                	".intranet_vle"
	                                ]},
	subject_combo : "#sel_subject_area",
	btn_search_subject_area : "#btn_search_subject_area",
	btn_search_category_area : "#btn_search_category_area",
	key_word_search : "#key_word_search",
	key_word_text : "search prc",
	btn_keyword_search : "#btn_keyword_search", 
	cmb_filter : "#cmb_filter",
	ProspectusList : "#ProspectusList",

	load_subject_areas: function  () { 
                  
			$("#subject_loading").css("display","block");
		    $(this.subject_combo).css('cursor','wait');
		 	$(this.subject_combo).attr('disabled', true);
		 	//$(this.subject_combo).css("background-color","silver");
			var new_category = ($(this.category_combo).val());
			if (new_category != this.category) {
				this.category = new_category;
				//
				$(this.subject_combo).children().remove();
				$(this.subject_combo).append(
						'<option  value=0>-- Select from... --</option>');
				//$(this.subject_combo)[0].options.length = 1;
				//alert (this.category + "" + section)
				//if (this.category == null) this.category = this.category_data["Categories"][section].value;
				var json_text = getFile(this.json_subjectarealist + "?category=" + this.category);
				var json = JSON.parse(json_text);
							
				for (_j in json["SubjectArea"]) {
					if (json["SubjectArea"][_j].Description != undefined) {
						var selected = "";
						//alert(getAjaxParameter("search_subject") + " " + json["SubjectArea"][_j].Code)
						if (getAjaxParameter("search_subject") == json["SubjectArea"][_j].Code) selected = "selected";
						//$(this.subject_combo).append( new Option(json["SubjectArea"][_j].Description, json["SubjectArea"][_j].Code, selected ));
						$(this.subject_combo)
							.append(
									'<option ' + selected 
									+ ' value=\"' + json["SubjectArea"][_j].Code + '\">' 
									+ json["SubjectArea"][_j].Description + '</option>');
					}
				}
							
			}
			$(this.subject_combo).attr('disabled', false);
			$(this.subject_combo).css('cursor','auto');
			//console.log(1);
			$("#subject_loading").hide("slow");
		},
		
		load_course_search_combos: function () {
			$(this.category_combo).attr('disabled', 'disabled');
			setAjaxParameter("study_year","2011");
			setAjaxParameter("category_search_enabled","on");
	        
			$(this.category_combo).children().remove();
			for (_c in this.category_data["Categories"]) {
				if (this.category_data["Categories"][_c].value != undefined) {
					var selected = "";
					if ((this.category_data["Categories"][section] != undefined)&&(this.category_data["Categories"][section].value == this.category_data["Categories"][_c].value)) selected = "selected";
					else if ((this.category_data["Categories"][area] != undefined)&&(this.category_data["Categories"][area].value == this.category_data["Categories"][_c].value)) selected = "selected";
					else selected = "";
                                        var  $disabled = "";
                                        if (this.category_data["Categories"][_c].disabled != undefined) $disabled = "disabled='disabled'";
                                        
					$(this.category_combo)
						.append(
									'<option ' + $disabled + " " + this.category_data["Categories"][_c].disabled + selected + ' value=\"' + this.category_data["Categories"][_c].value + '\">' 
										+ this.category_data["Categories"][_c].text + '</option>');
					//$(this.category_combo).append( new Option(this.category_data["Categories"][_c].text, this.category_data["Categories"][_c].value, selected ));
				}	
			}
			$(this.category_combo).removeAttr('disabled');

		},
		
		course_search: function () {
			
			
			//Get category area
			var category_area = "";
			var category = $(this.category_combo).val();
			for (_c in this.category_data["Categories"])  
				if (category == this.category_data["Categories"][_c].value)
					category_area = this.category_data["Categories"][_c].area;
			
			
			setAjaxParameter("search_category",category);
			setAjaxParameter("search_subject",$(this.subject_combo).val());
			
			var key_words = $(this.key_word_search).val();
			if (key_words == this.key_word_text) setAjaxParameter("search_key_words", "");
			else setAjaxParameter("search_key_words", key_words);
			
			window.location = "/" + category_area + "/search.php";
		   
		    
		    
		    

			
			
		},
		filter_courses : function () {
			$(this.ProspectusList).val("x");
			setAjaxParameter("search_mode", $(this.cmb_filter).val());
			window.location.reload();
		},
		
		load_filter_options : function () {
			if ($(this.cmb_filter).length>0) {
			var selected_filter = getAjaxParameter("search_mode");
			$(this.cmb_filter)[0].options.length = 0;
			for (_o in this.filter_options["options"]) {
				var selected = false;
				if (this.filter_options["options"][_o].value != undefined) {
					if (selected_filter == this.filter_options["options"][_o].value) selected = "selected";
					//$(this.cmb_filter).append( new Option(this.filter_options["options"][_o].text, this.filter_options["options"][_o].value, selected ));
					$(this.cmb_filter)
					.append(
								'<option ' + selected + ' value=\"' + this.filter_options["options"][_o].value + '\">' 
									+ this.filter_options["options"][_o].text + '</option>');
					
					
					
				}
				
			}
			}
		},
		back_to_search_list : function () {
			$("#content_text").html("Loading course list...");
			location.reload();
		},
		
		goProspectus : function (prospectus_code, start_date) {
			var ProspectusList = document.getElementById("ProspectusList");
			ProspectusList.innerHTML = "Searching for course details...";
			setAjaxParameter("ProspectusCode",prospectus_code);
                        window.sessionStorage.ProspectusCode = prospectus_code;
                        window.sessionStorage.start_date = start_date;                      
			
			$("#attendence_filter").hide("slow");
			$("#course_search_title").hide("slow");
			
			var xml = getFile("/modules/search_box/json_obtainprospectus.php");
			var json = JSON.parse(xml);
			
			
			//Print course details
			var op_text = "<div id=course_details>";
			op_text += "<h2>" + json["Details"]["Heading"] + "</h2>";
			op_text += "<div class=course_details_summary>";
				
			op_text += "<span class=strong>Level: </span><span class=normal>" + json["Details"]["Level"] + "</span>";
			op_text += "<span class=strong> Subject Area: </span><span class=normal>" + json["Details"]["SubjectArea"] + "</span>";
			op_text += "<span class=strong> Curriculum Area: </span><span class=normal>" + json["Details"]["CurriculumArea"] + "</span>";
			
			op_text += "</div>";
			
			
			var count=0;

			
			for (_d in json["Sections"]["Section"]) {
				if (json["Sections"]["Section"][_d]["Heading"] != undefined) {
					op_text += "<div class=course_details_heading>" + json["Sections"]["Section"][_d]["Heading"] + "</div>";
					//if (json__obj_count(json["Sections"]["Section"][_d]["Content"]) > 0) 
					
					
					op_text += "<div class=course_details_content>";
					
					if (typeof json["Sections"]["Section"][_d]["Content"] !== "object") op_text +=  json["Sections"]["Section"][_d]["Content"];
					else for (x in json["Sections"]["Section"][_d]["Content"]) 
						if (typeof json["Sections"]["Section"][_d]["Content"][x] !== "function") {
							if (typeof json["Sections"]["Section"][_d]["Content"][x] != "object") op_text += "<br>" + json["Sections"]["Section"][_d]["Content"][x];
							else for (w in json["Sections"]["Section"][_d]["Content"][x]) op_text +=  json["Sections"]["Section"][_d]["Content"][x][w];
						}
					op_text +=  "</div>";
					
					
				}
			}
			
			
			
			op_text += "</div>";
			
			if (json["LearningAims"] != undefined) {
			//Print Coasts
			op_text += "<div id=course_aims_div>";
			
			
			op_text += 	"<div class=aim_title>";
			if (json["LearningAims"]["Programme"] != undefined) {
				op_text += "<div class=aim_title_left>" + json["LearningAims"]["Programme"]["Title"] + "</div>";
				op_text +=	"<div class=aim_title_right>" + json["LearningAims"]["Programme"]["Code"] + "</div>";
			}
			op_text += 	"<div class=aim_details_heading>";
			op_text += 		"<div class=aim_cost>Fees*</div>";
			op_text += 	"</div>";
			
			if (json["LearningAims"]["Programme"] != undefined) {
			op_text += 	"<div class=aim_details>";
			op_text += 		"<div class=aim_cost>" + json["LearningAims"]["Programme"]["TotalFee"] + "</div>";
			op_text += 	"</div>";
			}
			
			op_text +=	"</div>";
			
			
			//Print learning aims
			for (_a in json["LearningAims"]["Aim"]) {
				if (json["LearningAims"]["Aim"][_a]["Title"] != undefined ) {
					op_text += "<div class=aim>";
					op_text += 	"<div class=aim_title>";
					op_text +=		"<div class=aim_title_left>" + json["LearningAims"]["Aim"][_a]["Title"] + "</div>";
					
					op_text +=		"<div class=aim_title_right>";
					if (typeof  json["LearningAims"]["Aim"][_a]["Code"] != "object" ) op_text +=  json["LearningAims"]["Aim"][_a]["Code"];
					else for (_c in json["LearningAims"]["Aim"][_a]["Code"]) op_text += json["LearningAims"]["Aim"][_a]["Code"][_c]; 
					op_text += "</div>";
								
					op_text +=	"</div>";
					op_text += 	"<div class=aim_details_heading>";
					op_text += 		"<div class=aim_weeks>Start date</div>";
					op_text += 		"<div class=aim_weeks>Number of Weeks</div>";
					op_text += 		"<div class=aim_attend>Attendance</div>";
					op_text += 		"<div class=aim_cost>Fees</div>";
					op_text += 	"</div>";
					op_text += 	"<div class=aim_details>";
					op_text += 		"<div class=aim_weeks>" + start_date  + "</div>";
					op_text += 		"<div class=aim_weeks>"  + json["LearningAims"]["Aim"][_a]["NumberOfWeeks"] + "</div>";
					
					op_text += 		"<div class=aim_attend>";
					if (typeof  json["LearningAims"]["Aim"][_a]["Attendance"] != "object" ) op_text +=  json["LearningAims"]["Aim"][_a]["Attendance"];
					else for (_sa in json["LearningAims"]["Aim"][_a]["Attendance"]) op_text +=  json["LearningAims"]["Aim"][_a]["Attendance"][_sa];
					
					op_text += "</div>";
					
					op_text += 		"<div class=aim_cost>" + json["LearningAims"]["Aim"][_a]["TotalFee"] + "</div>";
					op_text += 	"</div>";
					op_text += "</div>";
				}
			}
			op_text += "</div>";
			
			
		   op_text += "<div id=course_aims>";
			
			//op_text += 	"<div class=aim_details>";
			
			op_text += 	"<div style='clear:both'>";
			op_text += 		"<div class=aim_cost_reduced>*Reduced Fees ";
			if (json["LearningAims"]["Programme"] != undefined)  op_text += "<div class=aim_cost_statement>" + json["LearningAims"]["Programme"]["ReducedFee"] + "</div>";
			op_text += 		"<br>(Please contact the Information Centre for eligibility rules)</div>";
			op_text += 	"</div>";
			}
			//op_text +=	"</div>";
			
			//Check for bullet points
			op_text = escape(op_text).replace(/%u2022/g,"<li>");
			op_text = unescape(op_text);
			//console.log(op_text);
			
			ProspectusList.innerHTML = op_text;
			
			set_height();
			
			var html_text = "";
			
			//html_text += "<div id=search_apply_now onclick=\"window.location='course_application.php'\">Apply now</div>\n";
			html_text += "<div id=search_print onclick=\"search_box.print_course()\">Print this course</div>\n";
			html_text += "<div id=search_back onclick=\"search_box.back_to_search_list()\">Back to course list</div>\n";
			
			
			$("#info_bar").html(html_text);
			
			
			//$("#info_bar").append("<div id=email_friend onclick=\"search_box.email_to_friend('" + prospectus_code + "')\">Email to a friend &rarr;</div>");
			/*
			$(".aim_details").each(function(){
				var h = $(this).css("height");
				$(this).children().each(function(){
					 $(this).css("height",h);
				});
			});
			*/
			
			//$(".prospectus_description").css("width", "720px");
		    //$("#content_text").css("width", "900px");
			//$("#ProspectusList").css("position","absolute");
			$("#ProspectusList").css("left","-250px");
			$("#ProspectusList").css("width","-650px");
			
			$("#navmenu").slideUp("slow");
		    $("#info_bar").slideDown("slow");
		    
		    $("#content").animate ({
				left: "0px",
				width: "650px"
			},600);
			
			window.scroll(0,0);
			
		},
		
		email_to_friend : function (code) {
			var result = getFile("/modules/email_friend/index.php?code=" + code)
			alert(result);
		},
		
		print_course : function () {
			
			for (_np in this.no_prints["no_print_areas"]) {
				if (typeof this.no_prints["no_print_areas"][_np] != "function") {
					//console.log 
					$(this.no_prints["no_print_areas"][_np]).slideUp("slow");
					
				}
			}
			$("#content").css("position","absolute");
			$("#content").css("color","black");
			$(".course_details_heading").css("color","black");
			$(".aim_details_heading").css("background-color","transparent");
			$("#content").animate ({
				top: "100px",
				width: "100%"
			},600);
			$("#content_text").animate ({
				width: "100%"
			},600);
			$("#page").css("position","absolute");
			$("#page").animate ({
				width: "100%",
				left: "0px"
			},600);
			
			$(".aim_cost").css("float","left");
			$(".aim_title_right").css("float","left");
			$("#course_aims").css("border","none");
			$(".aim_cost").css("border","none");
			$(".aim_weeks").css("border","none");
			$(".aim_attend").css("border","none");
			$("#course_aims").css("border","none");
                        
                        
                        //html = "<button id=#backtocourse onclick=\"search_box.goProspectus(window.sessionStorage.ProspectusCode,window.sessionStorage.start_date);\">Back</button>";
                        //$(html).appendTo("#content_text");
                        html = "<div id=backtocourse ";
                        html += "class=\"noprint\" onclick=\"window.location.reload();\">Back to course list</div>";
                        $("#content_text").prepend(html);
			
			
		},
		
		show_note : function (l, t, text) {
			l -= parseInt($("#content").offset().left) - 20;
			t -= parseInt($("#content").css("top"));
			$('#type_note').stop();
			//alert($(this).css("top"));
			$('#type_note').html(text);
			$('#type_note').animate ({
				left: l,
				top: t,
				height:"40px",
				width:"100px"},500);
		},
		
		hide_note : function () {
			$('#type_note').stop();
			$('#type_note').animate ({height:"0px",width:"0px"},500);
		},
		
		init : function () {
			$(document).ready(function(){
				search_box.load_course_search_combos();
				//search_box.load_subject_areas();
				search_box.load_filter_options();
				//$("#ProspectusList").slideDown("slow");
				
				/*$(".type_note").mouseover (function (e) {
					search_box.show_note(e.pageX, e.pageY , $(this).attr("mode"));
					});*/
                            
                               $(".type_note").mouseover(function (){
                                   $(this).children("div").show();
                               })
                               $(".type_note").mouseout(function (){
                                   $(this).children("div").hide();
                               })
				
			});
			//if (window.attachEvent) window.attachEvent("onload", function(){search_box.load_course_search_combos();});
			//else if (window.addEventListener) window.addEventListener("load", function(){search_box.load_course_search_combos();}, false);
			
			$(this.category_combo).change(function() {search_box.load_subject_areas();});
			//$(this.subject_combo).click(function() {search_box.load_subject_areas();});
			
			/*
			*/
			//$(this.subject_combo).click(function() {

				
			 //   search_box.load_subject_areas();
				
			//});
			//$(this.subject_combo).load(function() {alert(1);$('body').css('cursor', 'wait')}); 
			
			
			
			$(this.btn_search_subject_area).click(function() {search_box.course_search();});
			$(this.btn_search_category_area).click(function() {search_box.course_search();});
			$(this.btn_keyword_search).click(function() {search_box.course_search();});
			$(this.key_word_search).val(this.key_word_text);	
			
			
			//search_box.load_subject_areas();
		}

}; 
search_box.init();

